# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=lensfun

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.2.8
pkgrel=2
pkgdesc="Database of photographic lenses and a library that allows advanced access to the database (mingw-w64)"
arch=('any')
url="http://lensfun.berlios.de/"
license=("LGPL3")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-python2")
depends=("${MINGW_PACKAGE_PREFIX}-glib2"
    "${MINGW_PACKAGE_PREFIX}-libpng"
    "${MINGW_PACKAGE_PREFIX}-libgnurx"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    )
options=('staticlibs' 'strip')
source=("http://download.berlios.de/${_realname}/${_realname}-${pkgver}.tar.bz2"
    lensfun-0.2.8-make-4.0.patch
    cmake-mingw.patch)
md5sums=('db2988505e7432c6b331aa597789c639'
         '9464d933a5e4ccefed7fc38656a9ec18'
         'a048b433caf35cce363c631fd6ad559b')

prepare(){
  cd "$srcdir/${_realname}-$pkgver"
  sed -i 's|#!/usr/bin/python|#!/usr/bin/python2|' configure
  # fix configure script for make 4.0
  patch -Np0 -i "${srcdir}/lensfun-0.2.8-make-4.0.patch"
  patch -p1 -i "${srcdir}/cmake-mingw.patch"
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  mkdir $srcdir/build-${MINGW_CHOST}
  cd $srcdir/build-${MINGW_CHOST}

  [[ $CARCH == x86_64 ]] && ENABLESSE2="-DBUILD_FOR_SSE2=ON"
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DBUILD_STATIC=OFF \
    -DBUILD_AUXFUN=ON \
    -DBUILD_TESTS=OFF \
    -DBUILD_FOR_SSE=ON \
    $ENABLESSE2 \
    ../${_realname}-${pkgver}

  make #VERBOSE=1
}

package() {
  cd "$srcdir/build-${MINGW_CHOST}"
  make install

  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  
  sed -i "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/lensfun.pc
}
